package Tools;

import java.io.OutputStream;
import java.io.PrintStream;

import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class MyPrintStream extends PrintStream {  
	 
private JTextArea text;
private StringBuffer sb = new StringBuffer();
   
   public MyPrintStream(OutputStream out, JTextArea text) {
	   //对输出流进行处理
       super(out);  
        this.text = text; 
   }
  
  /** 
     * 在这里重截,所有的打印方法都要调用的方法 
     */  
       @Override
    public void write(byte[] buf, int off, int len) {  
         final String message = new String(buf, off, len);   
         SwingUtilities.invokeLater(new Runnable(){
         public void run(){
          sb.append(message+"\n");
          text.setText(sb.toString());
          text.paintImmediately(text.getBounds());
         }
      });
   }
}